---
import { MatrixTable } from '../components/MatrixTable';
import { getEntry, type CollectionEntry } from 'astro:content';
import { getIsDev } from '@utils/env';
import { getFrameworkFromPath } from '@components/docs/utils/urlPaths';

interface Props {
    /**
     * Data file name within `src/content/matrix-table`
     *
     * Excluding the extension
     */
    dataFileName: string;
}

const { dataFileName, ...props } = Astro.props as Props;

let data;
const entry = (await getEntry('matrixTable', dataFileName)) as CollectionEntry<'matrixTable'>;
const contentFolder = 'matrix-table';

if (entry) {
    data = entry.data;
} else {
    const errorMsg = dataFileName.endsWith('.json')
        ? '`dataFileName` does not require `.json` extension. Use the name of the file'
        : `Data file "/src/content/${contentFolder}/${dataFileName}" not found`;
    if (getIsDev()) {
        console.error(errorMsg);
    } else {
        throw new Error(errorMsg);
    }
}

const framework = getFrameworkFromPath(Astro.url.pathname);
---

{data && <MatrixTable framework={framework} data={data} {...props} />}
